package RegistrationAndLogin;

import java.util.Scanner;

public class UserRegistration {
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        String username = "";
        String password = "";
        boolean registrationSuccessful = false;
        while(!registrationSuccessful)
        {
            System.out.println("请输入用户名：");
            username = sc.nextLine();
            System.out.println("请输入密码：");
            password = sc.nextLine();
            System.out.println("请确认密码：");;
            String confirmPassword = sc.nextLine();
            if(password.equals(confirmPassword))
            {
                System.out.println("注册成功！");
                registrationSuccessful = true;
            }
            else
            {
                System.out.println("密码不一致，请重新注册");
            }
        }

        int loginAttempts = 0;
        boolean loginSuccessful = false;
        while(loginAttempts < 3 && !loginSuccessful)
        {
            System.out.println("请输入用户名：");
            String loginUsername  = sc.nextLine();
            System.out.println("请输入密码：");
            String loginPassword = sc.nextLine();
            if(username.equalsIgnoreCase(loginUsername) && password.equalsIgnoreCase(loginPassword))
            {
                System.out.println("登录成功！");
                loginSuccessful = true;
                openCalculator();
            }
            else
            {
                loginAttempts++;
                if(loginAttempts < 3)
                {
                    System.out.println("密码错误，请重新输入！");
                }
                else
                {
                    System.out.println("登录失败，无法再继续登录");
                }
            }
        }
        sc.close();
    }
    private static void openCalculator()
    {
        try
        {
            Runtime.getRuntime().exec("calc.exe");
        }
        catch(Exception e)
        {
            System.out.println("无法打开计算器："+e.getMessage());
        }
    }
}
